//
//  CheckInViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/5.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "CheckInViewModel.h"

#import "ActiveMissionCellModel.h"

@interface CheckInViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;
@property (nonatomic, readwrite, strong) RACCommand* requestSigninInfoCmd;
@property (nonatomic, readwrite, strong) RACCommand* requestSigninCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation CheckInViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.checkCount = 0;
    
    self.cellHeight = 72.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 选中
    //    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(ArticleItemViewModel * itemViewModel) {
    //        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    //            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
    //                [subscriber sendNext:itemViewModel];
    //                [subscriber sendCompleted];
    //            });
    //            return nil;
    //        }];
    //    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
    
    self.requestSigninInfoCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                             {
                                 @strongify(self);
                                 
                                 return [self requestSignInfoSignal];
                             }];
    
    self.requestSigninCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        return [self requestSignSignal];
    }];
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_signinactivitylist params:interface_param_signinactivitylist(um.userData.userID)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             NSArray* datas = @[@(page*3), @(page*3 + 1), @(page*3 + 2)];
             
             /// 转化数据
             @weakify(self);
             NSArray *viewModels = [datas.rac_sequence map:^ActiveMissionCellModel *(NSNumber* number) {
                 @strongify(self);
                 
                 //计算当前cellheight
                 ActiveMissionCellModel *itemViewModel = [[ActiveMissionCellModel alloc] initWithData:[NSString stringWithFormat:@"活动任务-%d", [number intValue]] cellHeight:self.cellHeight];
                 
                 return itemViewModel;
             }].array;
             
             if (self.shouldPullUpToLoadMore)
             {
                 if (0 == page)
                 {
                     self.dataSource = viewModels;
                 }
                 else
                 {
                     if (self.dataSource == nil)
                     {
                         self.dataSource = viewModels;
                     }
                     else
                     {
                         self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                     }
                 }
             }
             else
             {
                 self.dataSource = viewModels;
             }
             
             [subscriber sendNext:x[SUCommandSysMessageKey]];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"签到活动列表获取失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

- (RACSignal *)requestSignInfoSignal
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_sign params:interface_param_sign(um.userData.userID)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             self.checkCount = [x[@"weekSignCount"] integerValue];
             
             [subscriber sendNext:x[SUCommandSysMessageKey]];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"请求签到界面信息失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

- (RACSignal*)requestSignSignal
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_signin params:interface_param_signin(um.userData.userID)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             self.checkCount = [x[@"signCount"] integerValue];
             
             UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
             [um updateUserInfo];
             
             [subscriber sendNext:x[@"integral"]];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"请求签到界面信息失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end
